/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.sys>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
  * Name: serializer.h
  *
  * Purpose: serializer implementation for 'COM' Functional
  *
  * Developer:
  *   wen.gu , 2020-06-12
  *
  * TODO:
  *
  ***************************************************************************/

  /******************************************************************************
   **    INCLUDES
   ******************************************************************************/
#ifndef __PANDA_COM_SERIALIZER_H__
#define __PANDA_COM_SERIALIZER_H__

#include <memory>
#include <vector>
#include <array>
#include <string>


#include "panda/sys/sys_types.h"
#include "panda/sys/ipc_types.h"

/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace panda
{
namespace sys 
{

namespace internal_impl
{
/** for check target has member function: void Serialize(Serializer& ser); */
template<typename T, typename... Args>
struct has_member_serialize
{
private:
    template<typename U> static auto Check(int) -> decltype(std::declval<U>().Serialize(std::declval<Args>()...), std::true_type());
    template<typename U> static std::false_type Check(...);
public:
    //static constexpr auto value = decltype(Check<T>(0))::value;
    enum { value = std::is_same<decltype(Check<T>(0)), std::true_type>::value };
};
} /** namespace internal_impl */
/******************************************************************************
**    CLASSES/FUNCTIONS DEFINITIONS
******************************************************************************/
/**class for serializer */
class SYS_CLASS Serializer final
{
public:
    Serializer();
    ~Serializer();

public:
    /** get payload */
    uint8_t*   payload();
    uint32_t   size();

public:
    void write(bool val);
    void write(int8_t val);
    void write(int16_t val);
    void write(int32_t val);
    void write(int64_t val);
    void write(uint8_t val);
    void write(uint16_t val);
    void write(uint32_t val);
    void write(uint64_t val);
    void write(float val);
    void write(double val);
    void write(const std::string& val);
    void write(const uint8_t* data, uint32_t size);
public:
    template<typename T>
    void write(const std::vector<T>& val)
    {/** treat Optional as dynamic array, |val.size()| element values...| */
        if (val.size() > 0)
        {
            write((uint32_t)val.size());
            for (auto it : val)
            {
                write(it);
            }
        }
        else
        {
            write(uint32_t(0));
        }
    }

    template<typename T, std::size_t N>
    void write(const std::array<T, N>& val)
    {
        if (val.size() > 0) /**here array not fully fill */
        {
            write((uint32_t)val.size());
            for (auto it : val)
            {
                write(it);
            }
        }
        else
        {
            write(uint32_t(0));
        }
    }

    template<typename T, class=typename std::enable_if<std::is_enum<T>::value >::type >
    void write(T val) /** write for enum */
    {
        write((typename internal_impl::enum2Int<sizeof(T)>::type)val);
    }

    template<typename T, class=typename std::enable_if<internal_impl::has_member_serialize<T, const Serializer&>::value >::type >
    void write(const T& val) /** write for struct */
    {/** every struct need provide a template method: 
      * template<typename T>
      * Serialize(const T& ser) const; 
      */
        val.Serialize(*this); /**todo, refineme */
    }

private:
    class impl;
    std::unique_ptr<impl> mImpl;
};


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

using SerializerPtr = std::shared_ptr<Serializer>;

} /** namespace sys */
} /** namespace panda */

#endif /** !__PANDA_COM_SERIALIZER_H__ */